<?php 
/**
 * 数据库自增自减
 **/
namespace App\Tools;
use App\Models\UserCheck;
use App\Models\WalletLog;
use App\Models\PointLog;
use App\Models\Order;
use App\Models\ShopKickback;
class MoneyChange{
    /**
     * 余额变动
     * $id=用户id,$money=变动金,$type=类型（o减 1加）,$sign=交易备注,$trace_way交易方式（0余额1微信2支付宝3其他）$note(事由)$order_id(对应的订单收取回扣)
     */
    public function  add_money($id,$money,$type=0,$sign='',$trace_way,$note='',$order_id=false){
        //检查用户
        if($money<0){
            return false;
        }
        $changeUser = UserCheck::find($id);
        if(!$changeUser){
            return false;
        }else{
            if($type==0){//减钱
                $changeUser->money = $changeUser->money - $money;
                $now_money = $changeUser->money;
                $changeUser->save();
            }else if($type==1){//加钱
                $changeUser->money = $changeUser->money + $money;
                $now_money = $changeUser->money;
                $changeUser->save();
            }else{
                return false;
            }
            $this->add_wallet_log($id,$money,$now_money,$type,$sign,$trace_way,$note);
            if($order_id){
                $this->collect_kickbacks($order_id,$id);
            }
            return true;
        }
    }
    /**
     * 添加钱包流水
     */
    public function  add_wallet_log($id,$money,$now_money,$type,$sign='',$trace_way=0,$note=''){
        WalletLog::create([
            'user_id'=>$id,
            'serial_number'=>randID(),
            'type'=>$type,
            'work_capital'=>$money,
            'now_money'=>$now_money,
            'sign' =>$sign,
            'note'=>$note,
            'trace_way'=>$trace_way,
        ]);
        return true;
    }

    /**
     *添加积分
     */
    public function add_point($id,$point,$type=0,$sign=''){
        //检查用户
        $changeUser = UserCheck::find($id);
        if(!$changeUser){
            return false;
        }else{
            if($type==0){//减钱
                $changeUser->point = $changeUser->point - $point;
                $now_point = $changeUser->point;
                $changeUser->save();
            }else if($type==1){//加钱
                $changeUser->point = $changeUser->point + $point;
                $now_point = $changeUser->point;
                $changeUser->save();
            }else{
                return false;
            }
            $this->add_point_log($id,$point,$now_point,$type,$sign);
            return true;
        }
    }

    /**
     * 添加积分流水
     */
    public function add_point_log($id,$point,$now_point,$type,$sign=''){
        PointLog::create([
            'user_id'=>$id,
            'type'=>$type,
            'work_point'=>$point,
            'now_point'=>$now_point,
            'sign' =>$sign,
        ]);
        return true;
    }

    /**
     * 收取商家回扣
     */
    public function collect_kickbacks($order_id=false,$id=false){
        if(!$order_id){
            return false;
        }
        $order = Order::find($order_id);
        if(!$order){
            return false;
        }else{
            //获取回扣百分比(数据库未写)
            $percentage = ShopKickback::where('shop_id',$order->shop_id)->first(['discount']);
            if($percentage){
                $percentage =  $percentage->discount;
            }else{
                $percentage = 0.05;//(默认0.05)
            }
            $money=sprintf("%.2f", $percentage*$order->order_price);
            if($money>0){
                $this->add_money($id,$money,0,'（服务费）订单编号：'.$order->order_number, 0,'服务费');
                return true;
            }
        }
        return false;
    }

}

